#ifndef REFLECTOR_H
#define REFLECTOR_H

#include <unordered_map>
#include <functional>
#include "TraitInfo.h"

namespace OpenRA::Game::Tools {

class Reflector
{
/***************************************************
 * 此段代码确保单例
 */
private:
	static Reflector *reflector_p;
	Reflector()= default;
public:

	static Reflector *Instance()
	{
		if (nullptr == reflector_p)
			reflector_p = new Reflector;
		return reflector_p;
	}

/***************************************************
 * 此段代码实现反射器
 */
private:
	std::unordered_map<std::string, std::function<OpenRA::Game::Trait::TraitInfo*()>> create_functions;

public:
	bool Register(const std::string& className, const std::function<OpenRA::Game::Trait::TraitInfo*()>& createFunc);
	OpenRA::Game::Trait::TraitInfo* Create(const std::string& className);
#define REFLECT_REGISTER(CLASS_NAME) OpenRA::Game::Tools::RegisterAction g_register_action_##CLASS_NAME(#CLASS_NAME, [](){ return new CLASS_NAME(); })
};

class RegisterAction
{
public:
	RegisterAction(const std::string &class_name, std::function<OpenRA::Game::Trait::TraitInfo*()> && generator)
	{
		Reflector::Instance()->Register(class_name, std::forward<std::function<OpenRA::Game::Trait::TraitInfo*()>>(generator));
	}
};

}



#endif //REFLECTOR_H
